package mall.dao;

import java.text.SimpleDateFormat;
import java.util.List;

import mall.pojo.Orders;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class OrderDAOImpl implements OrderDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void createOrder(Orders orders) {
		sessionFactory.getCurrentSession().save(orders);
	}

	@Override
	public int updateOrder(Orders orders) {
		String hql = "UPDATE Orders SET address =:address "
				+ ", email =:email , fullname = :fullname , note=:note,"
				+ " phone=:phone, state=:state, payment_method=:payment_method WHERE id=:id";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter("address", orders.getAddress());
		query.setParameter("email", orders.getEmail());
		query.setParameter("fullname", orders.getFullname());
		query.setParameter("note", orders.getNote());
		query.setParameter("phone", orders.getPhone());
		query.setParameter("state", orders.getState());
		query.setParameter("payment_method", orders.getPaymentmethod());
		query.setParameter("id", orders.getId());
		return query.executeUpdate();
	}

	@Override
	public List<Orders> getOrderShop(int shopId) {
		String hql = "FROM Orders O WHERE O.shop.id = '" + shopId + "'";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}

	@Override
	public Orders getOrder(int orderId) {
		String hql = "FROM Orders O WHERE O.id = '" + orderId + "'";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		List<Orders> orders = query.list();
		if (orders.size() > 0) {
			return orders.get(0);
		}
		return null;
	}

	@Override
	public int deleteOrder(int orderId) {
		// TODO Auto-generated method stub
		return 0;
	}

}
